package com.baidu.lib_common.animator

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation

var bottomRotateAngle: Float = 0f
var rotateAnimation: ObjectAnimator ?= null
fun View.rotate(){

    val actualStartAngle = bottomRotateAngle
    // 结束角度 = 起始角度 + 360度（确保刚好一圈）
    val endAngle = actualStartAngle + 360f

    val animator = ObjectAnimator.ofFloat(
        this,
        "rotation",
        actualStartAngle,
        endAngle
    ).apply {
        duration = 8000
        repeatCount = ValueAnimator.INFINITE
        interpolator = LinearInterpolator()
        repeatMode = ValueAnimator.RESTART
        start()
    }
    rotateAnimation = animator
}
fun View.stopRotate(){
    if(rotateAnimation != null){
        bottomRotateAngle = rotateAnimation!!.getAnimatedValue("rotation") as Float
        rotateAnimation!!.cancel()
        rotateAnimation = null
    }
}
fun View.toRotateAngle(){
    this.rotation = bottomRotateAngle
}
fun View.rotate20Angle(){
    val animator = ObjectAnimator.ofFloat(
        this,
        "rotation",
        0f,
        25f
    ).apply {
        pivotX = 0f
        pivotY = 0f
        duration = 2000
        repeatMode = ValueAnimator.RESTART
        start()
    }
}
fun View.rotateAbs20Angle(){
    val animator = ObjectAnimator.ofFloat(
        this,
        "rotation",
        25f,
        0f
    ).apply {
        pivotX = 0f
        pivotY = 0f
        duration = 2000
        repeatMode = ValueAnimator.RESTART
        start()
    }
}